package com.caird.common.utils;

public class HexUtils {

    public static String encode(byte b) {
        int high = (b >> 4) & 0xFF;
        int low = b & 0xFF;
        return Integer.toHexString(high) + Integer.toHexString(low);
    }

    /**
     * 字节流转Hex字符串
     */
    public static String encode(byte bytes[]) {
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < bytes.length; i++) {
            sb.append(encode(bytes[i]));
        }
        return sb.toString().trim();
    }

    int value = Integer.parseInt("18",16);

    /**
     * Hex字符串转字节流
     */
    public static byte[] decode(String s) {
        byte[] bytes = new byte[s.length()/2];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte)Integer.parseInt(s.substring(2*i, 2*i+2), 16);
        }
        return bytes;
    }
}
